package com.ihr360.common;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ihr360.commons.lang.DateUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

public class DateRange implements Serializable {
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8" )
    private Date start;
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8" )
    private Date end;

    public DateRange() {
    }

    public DateRange(Date start, Date end) {
        this.start = start;
        this.end = end;
    }

    public Date getStart() {
        return start;
    }

    public Date getEnd() {
        return end;
    }
    @JsonIgnore
    public int getFromYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        return calendar.get(Calendar.YEAR);
    }
    @JsonIgnore
    public int getFromMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        return calendar.get(Calendar.MONTH) + 1;
    }
    @JsonIgnore
    public int getToYear() {
        if (end==null){
            return 0;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(end);
        return calendar.get(Calendar.YEAR);
    }
    @JsonIgnore
    public int getToMonth() {
        if (end==null){
            return 0;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(end);
        return calendar.get(Calendar.MONTH) + 1;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        DateRange dateRange = (DateRange) o;

        if (start != null ? !equals(start, dateRange.start) : dateRange.start != null) return false;
        return !(end != null ? !equals(end, dateRange.end) : dateRange.end != null);

    }
    @JsonIgnore
    public boolean isSameSartDate(DateRange dateRange){
        if (this == dateRange) return true;
        String startStr = DateUtils.formatDateStr(this.start, DateUtils.STANDARD_DATE_FORMAT_YMD);
        String anotherStartStr = DateUtils.formatDateStr(dateRange.start, DateUtils.STANDARD_DATE_FORMAT_YMD);
        return startStr.equals(anotherStartStr);
    }

    private boolean equals(Date date, Date anotherdate) {
        String startStr = DateUtils.formatDateStr(date, DateUtils.STANDARD_DATE_FORMAT_YMD);
        String anotherdateStr = DateUtils.formatDateStr(anotherdate, DateUtils.STANDARD_DATE_FORMAT_YMD);
        return startStr.equals(anotherdateStr);
    }

    @Override
    public int hashCode() {
        int result = start != null ? start.hashCode() : 0;
        result = 31 * result + (end != null ? end.hashCode() : 0);
        return result;
    }

    public static DateRange build(Date start, Date end) {
        return new DateRange(start, end);
    }

    public static DateRange union(DateRange dateRange, DateRange anotherRange) {
        Date start = dateRange.getStart().before(anotherRange.getStart()) ? dateRange.getStart() : anotherRange.getStart();
        Date end = dateRange.getEnd().after(anotherRange.getEnd()) ? dateRange.getEnd() : anotherRange.getEnd();
        return build(start, end);
    }
}